package com.berrontech.asbog;

import android.content.Context;
import android.content.Intent;

import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Create by levent8421 2021/4/9 1:12
 * NavigationBarUtils
 * Navigation Bar utils
 *
 * @author levent8421
 */
public class NavigationBarUtils {
    private static final String CMD_SHOW_STATUS_BAR = "settings put system systembar_hide 0";
    private static final String CMD_HIDE_STATUS_BAR = "settings put system systembar_hide 1";
    private static final Intent INTENT_TOGGLE_STATUS_BAR = new Intent("com.tchip.changeBarHideStatus");

    public static int hideNavigation(Context context) {
        final int res = execRootCmdSilent(CMD_HIDE_STATUS_BAR);
        context.sendBroadcast(INTENT_TOGGLE_STATUS_BAR);
        return res;
    }

    public static int showNavigation(Context context) {
        final int res = execRootCmdSilent(CMD_SHOW_STATUS_BAR);
        context.sendBroadcast(INTENT_TOGGLE_STATUS_BAR);
        return res;
    }

    private static int execRootCmdSilent(String cmd) {
        int result = -1;
        DataOutputStream dos = null;

        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
